﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.WindowsMobile.Samples.Location;
using GeoTrans;
namespace SiGPS
{
    public partial class MainForm : Form
    {
        private Gps gpsDevice;
        private GpsPosition lastPosition;
        private Point lastUtmPosition;
        private GpsDeviceState lastState;

        private LocationChangedEventHandler locationChangedEventHandler;
        private DeviceStateChangedEventHandler deviceStateChangedEventHandler;
        private EventHandler updateDataEventHandler = null;

        public MainForm()
        {
            InitializeComponent();

            gpsDevice = new Gps();
            locationChangedEventHandler = new LocationChangedEventHandler(gpsDevice_LocationChanged);
            deviceStateChangedEventHandler = new DeviceStateChangedEventHandler(gpsDevice_DeviceStateChanged);
            updateDataEventHandler = new EventHandler(updateDataHandler);
        }

        void gpsDevice_LocationChanged(object sender, LocationChangedEventArgs args)
        {
            this.lastPosition = args.Position;
            if (lastPosition.LongitudeValid && lastPosition.LatitudeValid && lastPosition.EllipsoidAltitudeValid)
            {
                double easting, northing;
                int zone;
                Converter.PolarWGS84ToUTMinED50(lastPosition.Latitude, lastPosition.Longitude,
                    lastPosition.EllipsoidAltitude, out easting, out northing, out zone);
                lastUtmPosition = new Point((int)easting, (int)northing);

            }
            else
            {
                lastUtmPosition = new Point();
            }
            this.Invoke(updateDataEventHandler);
        }

        void gpsDevice_DeviceStateChanged(object sender, DeviceStateChangedEventArgs args)
        {
            this.lastState = args.DeviceState;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (!gpsDevice.Opened)
            {
                timer.Enabled = true;
                progressBar.Value = 0;
                progressBar.Visible = true;
                btnUpdate.BackColor = Color.Green;

                gpsDevice.Open();
                gpsDevice.DeviceStateChanged += deviceStateChangedEventHandler;
                gpsDevice.LocationChanged += locationChangedEventHandler;
            }
            else
            {
                timer.Enabled = false;
                progressBar.Value = 0;
                progressBar.Visible = false;
                btnUpdate.BackColor = Color.Red;
                if (gpsDevice.Opened)
                {
                    gpsDevice.LocationChanged -= locationChangedEventHandler;
                    gpsDevice.DeviceStateChanged -= deviceStateChangedEventHandler;
                    gpsDevice.Close();
                }
            }
        }

        private void MainForm_Closing(object sender, CancelEventArgs e)
        {
            if (gpsDevice.Opened) gpsDevice.Close();
        }

        private void timerEsperaGps_Tick(object sender, EventArgs e)
        {
            int step = 10;
            if (progressBar.Value + step < progressBar.Maximum)
            {
                progressBar.Value += step;
            }
            else
            {
                progressBar.Value = 0;
            }
            Win32.SystemIdleTimerReset();
        }

        private void updateDataHandler(object sender, EventArgs e)
        {
            if (lastPosition != null)
            {
                progressBar.Value = 0;
                showCoordinateData();
            }
        }

        private void showCoordinateData()
        {
            lblHDLOP.Text = lastPosition.HorizontalDilutionOfPrecisionValid ? lastPosition.HorizontalDilutionOfPrecision.ToString("F2") : ".";
            lblVdlop.Text = lastPosition.VerticalDilutionOfPrecisionValid ? lastPosition.VerticalDilutionOfPrecision.ToString("F2") : ".";
            lblPdlop.Text = lastPosition.PositionDilutionOfPrecisionValid ? lastPosition.PositionDilutionOfPrecision.ToString("F2") : ".";
            lblLat.Text = lastPosition.LatitudeValid ? lastPosition.Latitude.ToString("F5") : "...";
            lblLong.Text = lastPosition.LongitudeValid ? lastPosition.Longitude.ToString("F5") : "...";
            lblHeight.Text = lastPosition.EllipsoidAltitudeValid ? lastPosition.EllipsoidAltitude.ToString("F5") : "...";
            lblTime.Text = lastPosition.TimeValid ? lastPosition.Time.ToLongTimeString() : "...";
            lblSpeed.Text = lastPosition.SpeedValid ? (lastPosition.Speed * 0.514).ToString("F2") : "...";
            lblHeading.Text = lastPosition.HeadingValid ? lastPosition.Heading.ToString("F0") : "...";
            lblSatSol.Text = lastPosition.SatelliteInSolutionCountValid ? lastPosition.SatelliteInSolutionCount.ToString() : ".";
            lblSatView.Text = lastPosition.SatellitesInViewCountValid ? lastPosition.SatellitesInViewCount.ToString() : ".";
            lblUtmX.Text = !lastUtmPosition.IsEmpty ? lastUtmPosition.X.ToString() : "...";
            lblUtmY.Text = !lastUtmPosition.IsEmpty ? lastUtmPosition.Y.ToString() : "...";
            lblFixCual.Text = lastPosition.FixQuality.ToString();
            lblFixType.Text = lastPosition.FisType.ToString();
        }



        private void btnMap_Click(object sender, EventArgs e)
        {
            //if (btnMap.Text == "Mapa" && lastPosition != null && lastPosition.LatitudeValid && lastPosition.LongitudeValid )
            //{
            //    btnMap.Text = "Coordenadas";
            //    PolarCoordinate g84Coord = new PolarCoordinate(lastPosition.Latitude, lastPosition.Longitude);
            //    TopoMap.GoogleMaps.GgMap googleMap = new TopoMap.GoogleMaps.GgMap(g84Coord, pictureBox1.Width);
            //    pictureBox1.Image = googleMap.Bmp;
            //    pictureBox1.Invalidate();                
            //    pictureBox1.BringToFront();
            //    pictureBox1.Visible = true;
            //}
            //else
            //{
            //    btnMap.Text = "Mapa";
            //    pictureBox1.SendToBack();
            //    pictureBox1.Visible = false;
            //}
        }
    }
}